import pandas as pd
import numpy as np
df=pd.read_csv("train_original.csv")
print([column for column in df])
area={i:j for i,j in zip(list(set(df["area_id"])),range(0,len(list(set(df["area_id"]))),1))}
df_test=df.iloc[600000:,:]
df_train=df.iloc[:600000,:]

for i in range(13):
    xr=70000
    df_1=df_train[df_train["is_5g"]==1]
    df_2=df_train[df_train["is_5g"]==0].sample(n=xr)
    df_cut_part=pd.concat([df_1,df_2])
    df_cut_part=df_cut_part.reset_index()
    df_cut_part=df_cut_part.drop("index",axis=1)
    sampler=np.random.permutation(xr+7938)
    df_cut_part=df_cut_part.take(sampler)
    if(i==0):
        df_cut=df_cut_part
    else:
        df_cut=pd.concat([df_cut,df_cut_part])


data=pd.DataFrame(df_cut,columns=['user_id','prov_id', 'area_id',  'chnl_type', 'service_type', 'product_type', 'innet_months', 'total_times', 'total_flux', 'total_fee', 'pay_fee', 'sex', 'age', 'manu_name', 'term_type', 'max_rat_flag', 'is_5g_base_cover', 'is_work_5g_cover', 'is_home_5g_cover', 'is_work_5g_cover_l01', 'is_home_5g_cover_l01', 'is_work_5g_cover_l02', 'is_home_5g_cover_l02', 'activity_type', 'is_act_expire', 'comp_type', 'call_days', 're_call10', 'short_call10', 'long_call10', 'bank_cnt', 'active_days01', 'active_days02', 'active_days03', 'active_days04', 'active_days05', 'active_days06', 'active_days07', 'active_days08', 'active_days09', 'active_days10', 'active_days11', 'active_days12', 'active_days13', 'active_days14', 'active_days15', 'active_days16', 'active_days17', 'active_days18', 'active_days19', 'active_days20', 'active_days21', 'active_days22', 'active_days23', 'game_app_flux', 'live_app_flux', 'video_app_flux', 'city_5g_ratio', 'city_level'])
label=df_cut.iloc[:,59:]
data_test=pd.DataFrame(df_test,columns=['user_id','prov_id', 'area_id',  'chnl_type', 'service_type', 'product_type', 'innet_months', 'total_times', 'total_flux', 'total_fee', 'pay_fee', 'sex', 'age', 'manu_name', 'term_type', 'max_rat_flag', 'is_5g_base_cover', 'is_work_5g_cover', 'is_home_5g_cover', 'is_work_5g_cover_l01', 'is_home_5g_cover_l01', 'is_work_5g_cover_l02', 'is_home_5g_cover_l02', 'activity_type', 'is_act_expire', 'comp_type', 'call_days', 're_call10', 'short_call10', 'long_call10', 'bank_cnt', 'active_days01', 'active_days02', 'active_days03', 'active_days04', 'active_days05', 'active_days06', 'active_days07', 'active_days08', 'active_days09', 'active_days10', 'active_days11', 'active_days12', 'active_days13', 'active_days14', 'active_days15', 'active_days16', 'active_days17', 'active_days18', 'active_days19', 'active_days20', 'active_days21', 'active_days22', 'active_days23', 'game_app_flux', 'live_app_flux', 'video_app_flux', 'city_5g_ratio', 'city_level'])
label_test=df_test.iloc[:,59:]
df_total=pd.DataFrame(df,columns=['user_id','prov_id', 'area_id',  'chnl_type', 'service_type', 'product_type', 'innet_months', 'total_times', 'total_flux', 'total_fee', 'pay_fee', 'sex', 'age', 'manu_name', 'term_type', 'max_rat_flag', 'is_5g_base_cover', 'is_work_5g_cover', 'is_home_5g_cover', 'is_work_5g_cover_l01', 'is_home_5g_cover_l01', 'is_work_5g_cover_l02', 'is_home_5g_cover_l02', 'activity_type', 'is_act_expire', 'comp_type', 'call_days', 're_call10', 'short_call10', 'long_call10', 'bank_cnt', 'active_days01', 'active_days02', 'active_days03', 'active_days04', 'active_days05', 'active_days06', 'active_days07', 'active_days08', 'active_days09', 'active_days10', 'active_days11', 'active_days12', 'active_days13', 'active_days14', 'active_days15', 'active_days16', 'active_days17', 'active_days18', 'active_days19', 'active_days20', 'active_days21', 'active_days22', 'active_days23', 'game_app_flux', 'live_app_flux', 'video_app_flux', 'city_5g_ratio', 'city_level'])

app=data.loc[:,"active_days01":'active_days23']
app["app_sum"]=app.sum(axis=1)
data=data.drop(['active_days01', 'active_days02', 'active_days03', 'active_days04', 'active_days05', 'active_days06', 'active_days07', 'active_days08', 'active_days09', 'active_days10', 'active_days11', 'active_days12', 'active_days13', 'active_days14', 'active_days15', 'active_days16', 'active_days17', 'active_days18', 'active_days19', 'active_days20', 'active_days21', 'active_days22', 'active_days23'],axis=1)
data["app_sum"]=app["app_sum"]

app_test=data_test.loc[:,"active_days01":'active_days23']
app_test["app_sum"]=app_test.sum(axis=1)
data_test=data_test.drop(['active_days01', 'active_days02', 'active_days03', 'active_days04', 'active_days05', 'active_days06', 'active_days07', 'active_days08', 'active_days09', 'active_days10', 'active_days11', 'active_days12', 'active_days13', 'active_days14', 'active_days15', 'active_days16', 'active_days17', 'active_days18', 'active_days19', 'active_days20', 'active_days21', 'active_days22', 'active_days23'],axis=1)
data_test["app_sum"]=app_test["app_sum"]

app_total=df_total.loc[:,"active_days01":'active_days23']
app_total["app_sum"]=app_total.sum(axis=1)
df_total=df_total.drop(['active_days01', 'active_days02', 'active_days03', 'active_days04', 'active_days05', 'active_days06', 'active_days07', 'active_days08', 'active_days09', 'active_days10', 'active_days11', 'active_days12', 'active_days13', 'active_days14', 'active_days15', 'active_days16', 'active_days17', 'active_days18', 'active_days19', 'active_days20', 'active_days21', 'active_days22', 'active_days23'],axis=1)
df_total["app_sum"]=app_total["app_sum"]



def handle_area(x):
    return area[x]
data['area_id']=data['area_id'].apply(handle_area)
df_total['area_id']=df_total['area_id'].apply(handle_area)
data_test['area_id']=data_test['area_id'].apply(handle_area)

data_train=data.iloc[:,1:]
data_train=(data_train-df_total.iloc[:,1:].mean())/df_total.iloc[:,1:].std()
data.iloc[:,1:]=data_train

data_train_test=data_test.iloc[:,1:]
data_train_test=(data_train_test-df_total.iloc[:,1:].mean())/df_total.iloc[:,1:].std()
data_test.iloc[:,1:]=data_train_test
print([column for column in data])

data=data.reset_index()
data=data.drop("index",axis=1)
keepfour=lambda x: float(round(x,5))
data.iloc[:,1:]=(data.iloc[:,1:]).applymap(keepfour)
data_test.iloc[:,1:]=(data_test.iloc[:,1:]).applymap(keepfour)
def to_int(x):
    return int(x)
label=label.applymap(to_int)
label_test=label_test.applymap(to_int)
data.to_csv("data_1.csv",index=0)
label.to_csv("label_1.csv",index=0)
data_test.to_csv("data_test_1.csv",index=0)
label_test.to_csv("label_test_1.csv",index=0)


